#ifndef DEFECTIDKEY_H_
#define DEFECTIDKEY_H_

#include <Arduino.h>
#include <ooPinChangeInt.h>

class DefectIdKey : public CallBackInterface
{
	public:
	uint8_t DefectId;
	
	
DefectIdKey(){};
DefectIdKey(uint8_t interupt_pin, uint8_t a0, uint8_t a1, uint8_t a2):interupt_pin(interupt_pin),a0(a0),a1(a1),a2(a2)
{
	init();
};

static unsigned long last_interrupt_time = 0;
void cbmethod()
{	
	unsigned long interrupt_time = millis();
	// If interrupts come faster than 200ms, assume it's a bounce and ignore
	if (interrupt_time - last_interrupt_time > 200)
	{
		DefectId = Dispatch(a0, a1, a2);
		last_interrupt_time = interrupt_time;
	}	
};

private:
uint8_t a0, a1, a2;
uint8_t interupt_pin;

void init()
{
	pinMode(interupt_pin, HIGH);
	digitalWrite(interupt_pin, HIGH);
	PCintPort::attachInterrupt(interupt_pin, this, FALLING);
};

uint8_t Dispatch(uint8_t bit0, uint8_t bit1, uint8_t bit2)
{
	if(bit0 == 0 && bit1 == 0 && bit2 == 0)
	return 0;
	else if(bit0 == 0 && bit1 == 0 && bit2 == 1)
	return 1;
	else if(bit0 == 0 && bit1 == 1 && bit2 == 0)
	return 2;
	else if(bit0 == 0 && bit1 == 1 && bit2 == 1)
	return 3;
	else if(bit0 == 1 && bit1 == 0 && bit2 == 0)
	return 4;
	else if(bit0 == 1 && bit1 == 0 && bit2 == 1)
	return 5;
	else if(bit0 == 1 && bit1 == 1 && bit2 == 0)
	return 6;
	else if(bit0 == 1 && bit1 == 1 && bit2 == 1)
	return 7;
	else return -1;
};
};



#endif /* DEFECTIDKEY_H_ */